﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using System.Windows.Forms;
using libDAL;

namespace libAddressBook.BUS
{
    public class clsBUSCustomerHistory
    {
        private string strTempConStr = @"Data Source=193.9.200.92\SERVERARCHIE;Initial Catalog=ESB_Enterprise;User ID=abc;Password=1234";

        private string strCompanyHistoryID = string.Empty;
        private string strCompanyID = string.Empty;
        private DataSet dsHistory;
        private DataTable dtTempHistory;
        private cls_DAL _DAL;

        public clsBUSCustomerHistory()
        {
            this._DAL = new cls_DAL(strTempConStr);
            getQueries = new Queries();
            dtTempHistory = new DataTable();
            dsHistory = new DataSet();
        }

        public cls_DAL get_DAL
        {
            get
            {
                return _DAL;
            }
        }

        struct Queries
        {
            public string getQueryCompanyHistoryNew
            {
                get
                {
                    return "SELECT companyHistoryID, comid, oldcomid, portid, comregno, comname, comname2, addbookcode, induscode, website, addline1, addline2, addline3, addline4, areacode, " +
                               "citycode, stacode, poscode, coucode, discode, email, connotepre, insper, insperfra, insminamt, minchaamt, contact, telpre, telno, faxpre, faxno, conope, slclotime, " +
                               "selected, regcust, glno, inscover, remarks, slotbook, bolprefix, holdgrn, holdrea, anote, nbl, fclinscover, fclinsper, fclinsperfra, fclinsminamt, fclstampduty, fclgovtax, " +
                               "memo, fcltermday, customcode, salperson, faxattn, markup, mpercent, byshipmea, allowvariance, cashbill, svbyemail, svbyfax, acccode, accid, billage, billbr, " +
                               "createdby, createddate, lastmodifiedby, lastmodifieddate, vip, minfrtrate, userreportprint, reportprintdate, reportprintby, hakgerakandate, svconsignee, autovesselsc, " +
                               "emailvesselsc, custgradecode, IsExportShprDO, mobilepre, mobileno, comtype, branchID, DateTimeInserted, DateTimeUpdated, HostNameIPAddress, OnUse, " +
                               "OnUseBy, comidHQ,comDefault,comStatus " +
                           "FROM mfcompanyHistory ";
                }
            }

            public string get_QueryCompanyHistory(string strCompanyID)
            {
                //get all the history of each branches
                return "SELECT companyHistoryID, comid, oldcomid, portid, comregno, comname, comname2, addbookcode, induscode, website, addline1, addline2, addline3, addline4, areacode, " +
                               "citycode, stacode, poscode, coucode, discode, email, connotepre, insper, insperfra, insminamt, minchaamt, contact, telpre, telno, faxpre, faxno, conope, slclotime, " +
                               "selected, regcust, glno, inscover, remarks, slotbook, bolprefix, holdgrn, holdrea, anote, nbl, fclinscover, fclinsper, fclinsperfra, fclinsminamt, fclstampduty, fclgovtax, " +
                               "memo, fcltermday, customcode, salperson, faxattn, markup, mpercent, byshipmea, allowvariance, cashbill, svbyemail, svbyfax, acccode, accid, billage, billbr, " +
                               "createdby, createddate, lastmodifiedby, lastmodifieddate, vip, minfrtrate, userreportprint, reportprintdate, reportprintby, hakgerakandate, svconsignee, autovesselsc, " +
                               "emailvesselsc, custgradecode, IsExportShprDO, mobilepre, mobileno, comtype, branchID, DateTimeInserted, DateTimeUpdated, HostNameIPAddress, OnUse, " +
                               "OnUseBy, comidHQ,comDefault,comStatus " + "FROM mfcompanyHistory " +
                       "WHERE substring(comidHQ,0,CHARINDEX('_', ltrim(rtrim(comidHQ)))) = '" + strCompanyID.Trim() + "' ";
            }
        }

        private Queries getQueries;

        private string getset_CompanyID
        {
            get
            {
                return strCompanyID;
            }
            set
            {

                strCompanyID = value;
            }
        }

        private string getset_CompanyHistoryID
        {
            get
            {
                return strCompanyHistoryID;
            }
            set
            {
                strCompanyHistoryID = value;
            }
        }

        public bool set_NewCompanyHistory()
        {
            bool bolStatus = false;
            try
            {
                dsHistory = new DataSet();
                bolStatus = _DAL.set_DataWCardinality_ds(getQueries.getQueryCompanyHistoryNew, ref dsHistory, "CompanyHistory-0");
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message + " set_NewCompanyHistory"); 
            }
            return bolStatus; 
        }

        public bool get_CheckCompanyHistoryExists()
        {
            bool bolStatus = false;
            try
            {
                int intTables = dsHistory.Tables.Count;

                if (intTables <= 0)
                {
                    bolStatus = true;
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message + " get_CheckCompanyHistoryExists"); 
            }

            return bolStatus;
        }

        private bool set_LastUpdate(DataRow drLastUpdate)
        {
            bool bolStatus = false;
            try
            {
                string strHistoryID = drLastUpdate["companyHistoryID"].ToString().Trim();

                    if (strHistoryID == string.Empty)
                    {
                        drLastUpdate["companyHistoryID"] = clsGenFunction.get_HistoryID(drLastUpdate["comid"].ToString ().Trim(), _DAL);
                        strHistoryID = drLastUpdate["companyHistoryID"].ToString().Trim();
                    }
                    DataRow[] drUpdate = this.dsHistory.Tables["CompanyHistory-0"].Select("companyHistoryID = '" + strHistoryID + "'");
                    //if there's existing company history ID then update it
                    if (drUpdate.Length > 0)
                    {
                        for (int intCol = 0; intCol < this.dsHistory.Tables["CompanyHistory-0"].Columns.Count; intCol++)
                        {
                            drUpdate[0][this.dsHistory.Tables["CompanyHistory-0"].Columns[intCol].ColumnName] = drLastUpdate[this.dsHistory.Tables["CompanyHistory-0"].Columns[intCol].ColumnName];
                        }
                        bolStatus = true;
                    }
                    else
                    {
                        //if no existing company add the datarow in data dataset
                        DataRow drtest = this.dsHistory.Tables["CompanyHistory-0"].NewRow();
                        for (int intCol = 0; intCol < this.dsHistory.Tables["CompanyHistory-0"].Columns.Count; intCol++)
                        {
                            drtest[intCol] = drLastUpdate[intCol];
                        }
                        this.dsHistory.Tables["CompanyHistory-0"].Rows.Add(drtest);
                    }
                    bolStatus = true;
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message + " set_LastUpdate"); 
            }
            return bolStatus;
        }

        public bool set_UpdateCustomerHistory(DataRow drLastUpdate)
        {
            bool bolStatus = false;
            try
            {
                if (set_LastUpdate(drLastUpdate) == true)
                {
                    //set_Bool_updateBatchNoAcceptChanges
                    if (_DAL.set_Bool_updateBatchNoAcceptChanges(ref this.dsHistory, "CompanyHistory-0") == true)
                    {
                        bolStatus = true;
                    }
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message + " set_UpdateCustomerHistory"); 
            }

            return bolStatus;
        }

        public bool set_InsertNewRow(DataRow drInsert)
        {
            bool bolStatus = false;
            try
            {
                dsHistory.Tables["CompanyHistory-0"].Rows.Add(drInsert);
                bolStatus = true;
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message + " set_InsertNewRow"); 
            }
            return bolStatus;
        }

        public bool AddNewRow(DataRow drInsert)
        {
            bool bolStatus = false;
            try
            {
                dsHistory.Tables["CompanyHistory-0"].Rows.Add(drInsert);
                bolStatus = true;
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message + " AddNewRow"); 
            }
            return bolStatus;
        }

        //this function update the details of spceific companyHistory as preparation for batch update (if nessesary)
        public bool set_SpecificCopanyDetails(string strCompanyHistoryID, DataRow drDetails)
        {
            bool bolStatus = false;
            try
            {
                DataRow[] drCheck = { };
                drCheck = dsHistory.Tables["CompanyHistory-0"].Select("companyHistoryID = '" + strCompanyHistoryID + "'");
                if (drCheck.Length > 0)
                {
                    for (int intCol = 0; intCol < dsHistory.Tables["CompanyHistory-0"].Columns.Count; intCol++)
                    {
                        drCheck[0][intCol] = drDetails[intCol];
                    }
                    bolStatus = true;
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message + " set_SpecificCopanyDetails");
            }
            return bolStatus;
        }

        public DataTable get_CompanyHistory(string strCompanyID)
        {
            DataTable dt = new DataTable();
            try
            {
                DataRow[] drchekc = dsHistory.Tables["CompanyHistory-0"].Select("comid= '" + strCompanyID + "'");
                if (drchekc.Length > 0)
                {
                    dt = drchekc.CopyToDataTable();
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message + " get_CompanyHistory"); 
            }
            return dt;
        }

        public DataTable get_CompanyAllHistory
        {
            get
            {
                return dsHistory.Tables["CompanyHistory-0"];
            }
        }

        public bool get_Records(string strHistoryID)
        {
            bool bolStatus = false;
            try
            {

            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message + " set_Records"); 
            }

            return bolStatus;
        }

        public bool set_Records(string strCompanyID)
        {
            bool bolStatus = false;
            try
            {
                //get all the history of each branches
                    getset_CompanyID = strCompanyID;
                    dsHistory = new DataSet();
                    if (this._DAL.set_DataWCardinality_ds(getQueries.get_QueryCompanyHistory(getset_CompanyID), ref dsHistory, "CompanyHistory-0") == true)
                    {
                        bolStatus = true;
                    }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message + " set_BindtoGrid");
            }
            return bolStatus;
        }

        public void set_Default(string strcompanyHistoryID, int intType)
        {
            try
            {
                DataRow[] drSet = dsHistory.Tables["CompanyHistory-0"].Select("companyHistoryID = '" + strcompanyHistoryID.Trim() + "'");
                if (drSet.Length > 0)
                {
                    drSet[0]["comDefault"] = intType;
                }
                drSet = dsHistory.Tables["CompanyHistory-0"].Select("companyHistoryID <> '" + strcompanyHistoryID.Trim() + "'");
                for (int intRow = 0; intRow < drSet.Length; intRow++)
                {
                    drSet[intRow]["comDefault"] = 0;
                }

                DataRow[] dr = dsHistory.Tables["CompanyHistory-0"].Select("comDefault > 0");
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message + " set_Default"); 
            }
        }

        internal void set_InitializeHistory()
        {
            dsHistory = new DataSet();
            //throw new NotImplementedException();
        }
    }
}
